package com.lmaos.basetools.files.scan;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ScanFileUtils {
	
	public static File getFile(String path){
		return new File(getFileString(path));
	}
	public static String getFilePathString(String path){
		int filePathIndex = path.lastIndexOf("!");
		if(filePathIndex !=-1){
			return path.substring(filePathIndex+1);
		}
		filePathIndex = path.lastIndexOf("?");
		if(filePathIndex !=-1){
			return path.substring(filePathIndex+1);
		}
		filePathIndex = path.lastIndexOf("/");
		if(filePathIndex !=-1){
			return path.substring(filePathIndex+1);
		}
		return null;
	}
	public static String getFileAbsoluteString(String path){
		int fileStartIndex = path.indexOf("file:");
		if(fileStartIndex != -1){
			path = path.substring(fileStartIndex+"file:".length());
		}
		if(path.startsWith("/")){
			int panIndex = path.indexOf(":");
			if(panIndex>1&&panIndex<=3){
				path = path.substring(1);
			}
		}
		return path;
	}
	public static String getFileString(String path){
		path = getFileAbsoluteString(path);
		int paramIndex = path.lastIndexOf("?");
		if(paramIndex == -1){
			paramIndex = path.lastIndexOf("!");
		}
		if(paramIndex != -1){
			path = path.substring(0, paramIndex);
		}
		return path.trim();
	}
	public static String getSuffixString(String path){
		int suffixIndex= path.lastIndexOf(".");
		if(suffixIndex != -1){
			return path.substring(suffixIndex+1);
		}
		return null;
	}
	/**
	 * 说明：创建相对文件路径或class名对应的索引		<br>
	 * 时间：2017年12月28日 下午5:05:44
	 *
	 * @param scanFiles
	 * @return
	 */
	public static Map<String, ScanFile> createFilePathOrClassName$ToScanFileSearchMap(List<ScanFile> scanFiles){
		Map<String, ScanFile>  searchMap = new HashMap<String, ScanFile>();
		for (ScanFile scanFile : scanFiles) {
			String className = scanFile.getClassName();
			String filePath = scanFile.getFilePath();
			searchMap.put(filePath, scanFile);
			if(className!=null){
				searchMap.put(className, scanFile);
			}
		}
		return searchMap;
	}
}
